package com.lgt_enterprise_android.view.pages;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.CommonUtil;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.KeyboardStateObserver;
import com.lgt_enterprise_android.utils.PrefUtils;
import com.lgt_enterprise_android.view.MainActivity;

public class Login2Activity extends BaseActivity {

    private View btnLogin;
    private EditText edPhone, edPass;
    private TextView txtTip;

    private View viewScroll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarColor = R.color.white;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);

        KeyboardStateObserver.getKeyboardStateObserver(this).setKeyboardVisibilityListener(new KeyboardStateObserver.OnKeyboardVisibilityListener() {
            @Override
            public void onKeyboardShow(int height) {
                CommonUtil.scrollToPos(0,height-dp2Px(150),viewScroll);
            }

            @Override
            public void onKeyboardHide(int height) {
                CommonUtil.scrollToPos(dp2Px(60),0,viewScroll);
            }
        });
        initView();
        bindEvent();
    }

    protected void initView() {
        viewScroll=findViewById(R.id.lin_scroll);
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setEnabled(false);
        edPhone = (EditText) findViewById(R.id.ed_phone_no);
        txtTip = (TextView) findViewById(R.id.txt_tip);
        edPass = (EditText) findViewById(R.id.ed_pass);
    }

    protected void bindEvent() {

        findViewById(R.id.txt_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        edPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                txtTip.setText("");
                if (edPhone.getText().toString().length() >0 && edPass.getText().toString().length() > 2) {
                    btnLogin.setEnabled(true);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button));
                } else {
                    btnLogin.setEnabled(false);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button_disable));
                }
            }
        });

        findViewById(R.id.txt_protocl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("url",Constant.PRIVACY_AGREEMENT);
                startActivity(intent);
            }
        });

        edPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                txtTip.setText("");
                if(edPhone.getText().toString().length()>0 && edPass.getText().toString().length()>2){
                    btnLogin.setEnabled(true);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button));
                }else{
                    btnLogin.setEnabled(false);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button_disable));
                }
            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CheckBox ch=(CheckBox)findViewById(R.id.ch);
                if(!ch.isChecked()){
                    showToast("请同意用户协议及隐私政策");
                    return;
                }


                String strPhone = edPhone.getText().toString().trim();

                showLoadingDialog();
                HttpManager.login(mContext, strPhone, edPass.getText().toString().trim(), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {


                        if (resonseObj instanceof JSONObject) {
                            JSONObject jo = (JSONObject) resonseObj;
                            Constant.token = jo.getString("token");

                            dismissLoadingDialog();
                            PrefUtils.setSharedPrefStringData(mContext, PrefUtils.TOKEN, Constant.token);

                            Intent intent = new Intent(mContext, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();

                        }

                    }

                    @Override
                    public void onFailure(String mes) {
                        dismissLoadingDialog();
                        txtTip.setText(mes);
                    }
                });
            }
        });

    }
}
